package org.xy.parser;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.xy.bean.Bean;
import org.xy.bean.BeanWapper;

/**
 * @author X.Y
 * 
 */
public class RootParser {
	protected final Logger logger = Logger.getLogger(this.getClass().getName());

	protected Map<String, Bean> beans = null;

	protected static Map<String, BeanWapper> beaWappers = new HashMap<String, BeanWapper>();

	public RootParser(Map<String, Bean> beans) {
		this.beans = beans;
	}

	protected boolean exist(String beanName) {
		return beaWappers.containsKey(beanName);
	}

	/**
	 * Used to overide by sub class to implement the get logic
	 * 
	 * @param beanName
	 * @return
	 */
	protected BeanWapper getBeanWapper(String beanName) {
		return null;
	}

	// FIXME Move the singleton check logic into sub class
	public Map<String, BeanWapper> getBeanWappers() {
		if (this.beans == null || this.beans.isEmpty()) {
			return null;
		}

		for (String beanName : this.beans.keySet()) {
			this.logger.info("get BeanWapper[" + beanName + "]");
			Bean bean = this.beans.get(beanName);
			BeanWapper beanWapper = null;
			if (bean.isSingleton()) {
				beanWapper = new BeanDefinitionSingletonParser(this.beans)
						.getBeanWapper(beanName);
			} else {
				beanWapper = new BeanDefinitionParser(this.beans)
						.getBeanWapper(beanName);
			}
			this.logger.info("BeanWapper[" + beanName + "] have ["
					+ beanWapper.childSize() + "] childs, its singleton is ["
					+ bean.isSingleton() + "]");

		}

		return beaWappers;
	}
}
